#=============================================================================#
#
# PROJECT:        Who Pays for Peace?
# AUTHORS:        ** anonymized for review **
# CONTACT:        ** anonymized for review **
# LAST MODIFIED:  July 5, 2022
# 
#=============================================================================#
#
# This R file contains the code used to replicate the descriptive statistics
# 
#=============================================================================#


# Initial settings ------------------------------------------------------------

#rm(list=ls())
#getwd()

## Install and load all necessary packages ------------------------------------
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.

ipak <- function(pkg){  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("dplyr", "tidyverse", "vtable", "psych") 
ipak(packages)


## Load and inspect the data --------------------------------------------------
mydata <- readRDS("data/T1-clean-data.rds")
str(mydata, list.len=ncol(mydata)) 


# Summary demographics (Table 2) ----------------------------------------------
sumstat <- mydata %>%
  
  # Select variables 
  dplyr::select(female, age, living, edu_2)
  
# Write .csv table with summary stats
sumtable(sumstat,
         summ = list(
           c('notNA(x)','mean(x)','sd(x)'),
           c('notNA(x)','mean(x)')
         ),
         summ.names = list(
           c('N','Mean','SD'),
           c('Count','Percent')
         ), out = "csv", file = "tables/sumstat.csv")

# Descriptive stats (footnote 20) ---------------------------------------------
describe(mydata$aggression)
describe(mydata$violence)


